import config from '../../../config/config.js'

// spin components
import Spin from './spin.js'

//  spinInstance spin 实例
let spinInstance

/**
 *
 * @param render
 * @returns {*|{component}}
 */
function getSpinInstance (render = undefined) {
  // 创建一个新的实例
  spinInstance = spinInstance || Spin.newInstance({
    render: render
  })
  // 返回实例
  return spinInstance
}

function loading (options) {
  const render = ('render' in options) ? options.render : undefined
  const instance = getSpinInstance(render)
  // 实例显示
  instance.show(options)
}

// show
Spin.show = function (props = {}) {
  return loading(props)
}

// hide
Spin.hide = function () {
  if (!spinInstance) {
    return false
  }
  const instance = getSpinInstance()
  // 实例移除
  instance.remove(() => {
    spinInstance = null
  })
}

export default Spin
